"""
人类反馈
"""
from core.data_analyze.state import AnalyzeState
from langgraph.types import Command, interrupt
from langchain_core.messages import HumanMessage
import re


def human_feedback_node(state: AnalyzeState):

    auto_decision = state.get('auto_decision')
    if not auto_decision:
        feedback = interrupt("> 分析条件不足，请重新叙述问题：")
        if feedback and re.search('[A-Za-z\u4E00-\u9F5A]', str(feedback)) is not None:
            return Command(
                update={"task_description": feedback},
                goto="task_rewrite",
            )
        else:
            return Command(
                update={"messages": HumanMessage(content=f'不正确的任务描述： {feedback}')},
                goto="human_feedback",
            )
    else:
        max_retry_times = state.get('max_retry_times')
        cur_retry_times = state.get('cur_retry_times')
        if cur_retry_times < max_retry_times:
            return Command(
                update={"cur_retry_times": cur_retry_times + 1},
                goto="task_rewrite",
            )
        else:
            return Command(goto="__end__")
